﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Smallerp.Contracts.DataContracts;

namespace SmallERP.PinBan.ImportPhoto.Forms
{
    public partial class FrmErrorPhotoCorrection : Form
    {

        List<PhotoDCT> _photoList = new List<PhotoDCT>();
        public FrmErrorPhotoCorrection(List<PhotoDCT> photoList)
        {
            InitializeComponent();
            _photoList = photoList;
        }

        private void FrmErrorPhotoCorrection_Load(object sender, EventArgs e)
        {
            cmbUnit.SelectedIndex = 0;
            BindPhotoInfoList();
        }

        private void BindPhotoInfoList()
        {
            gcPhoto.DataSource = _photoList;
            gcPhoto.RefreshDataSource();
        }

        private void gvPhoto_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            PhotoDCT photo = gvPhoto.GetRow(e.RowHandle) as PhotoDCT;

            if (e.Column.FieldName == "Width")//修改宽度
            {
                if (cmbUnit.Text == "厘米")
                {
                    photo.Width =Math.Round( Convert.ToDouble( e.Value) / 2.54,2);
                    photo.DPI = Convert.ToInt32(photo.PixWidth / photo.Width);
                    photo.Height =Math.Round( Convert.ToDouble(photo.PixHeight / photo.DPI),2);
             
                    if (photo.Height > photo.Width)
                    {
                        photo.SizeCM = string.Format("{0:0.##}", photo.Height * 2.54) + "X" +
                                           string.Format("{0:0.##}", photo.Width * 2.54);
                        photo.SizeInch = string.Format("{0:0.##}", photo.Height) + "X" +
                                             string.Format("{0:0.##}", photo.Width);
                    }
                    else
                    {
                        photo.SizeCM = string.Format("{0:0.##}", photo.Width * 2.54) + "X" +
                                       string.Format("{0:0.##}", photo.Height * 2.54);
                        photo.SizeInch = string.Format("{0:0.##}", photo.Width) + "X" +
                                         string.Format("{0:0.##}", photo.Height);
                    }
                }
                else//单位英寸
                {
                    photo.Width =Math.Round( Convert.ToDouble( e.Value),2);
                    photo.DPI = Convert.ToInt32(photo.PixWidth / photo.Width);
                    photo.Height =Math.Round( Convert.ToDouble(photo.PixHeight / photo.DPI),2);
                    if (photo.Height > photo.Width)
                    {
                        photo.SizeCM = string.Format("{0:0.##}", photo.Height * 2.54) + "X" +
                                           string.Format("{0:0.##}", photo.Width * 2.54);
                        photo.SizeInch = string.Format("{0:0.##}", photo.Height) + "X" +
                                             string.Format("{0:0.##}", photo.Width);
                    }
                    else
                    {
                        photo.SizeCM = string.Format("{0:0.##}", photo.Width * 2.54) + "X" +
                                       string.Format("{0:0.##}", photo.Height * 2.54);
                        photo.SizeInch = string.Format("{0:0.##}", photo.Width) + "X" +
                                         string.Format("{0:0.##}", photo.Height);
                    }
                }
            }
            else if (e.Column.FieldName =="Height")
            {
                if (cmbUnit.Text == "厘米")
                {
                    photo.Height =Math.Round( Convert.ToDouble( e.Value) / 2.54,2);
                    photo.DPI = Convert.ToInt32(photo.PixHeight / photo.Height);
                    photo.Width =Math.Round( Convert.ToDouble(photo.PixWidth / photo.DPI),2);
                    
                    if (photo.Height > photo.Width)
                    {
                        photo.SizeCM = string.Format("{0:0.##}", photo.Height * 2.54) + "X" +
                                           string.Format("{0:0.##}", photo.Width * 2.54);
                        photo.SizeInch = string.Format("{0:0.##}", photo.Height) + "X" +
                                             string.Format("{0:0.##}", photo.Width);
                    }
                    else
                    {
                        photo.SizeCM = string.Format("{0:0.##}", photo.Width * 2.54) + "X" +
                                       string.Format("{0:0.##}", photo.Height * 2.54);
                        photo.SizeInch = string.Format("{0:0.##}", photo.Width) + "X" +
                                         string.Format("{0:0.##}", photo.Height);
                    }
                }
                else//单位英寸
                {
                    photo.Height =Math.Round( Convert.ToDouble( e.Value),2);
                    photo.DPI = Convert.ToInt32(photo.PixHeight / photo.Height);
                    photo.Width =Math.Round( Convert.ToDouble(photo.PixWidth / photo.DPI),2);
                   
                    if (photo.Height > photo.Width)
                    {
                        photo.SizeCM = string.Format("{0:0.##}", photo.Height * 2.54) + "X" +
                                           string.Format("{0:0.##}", photo.Width * 2.54);
                        photo.SizeInch = string.Format("{0:0.##}", photo.Height) + "X" +
                                             string.Format("{0:0.##}", photo.Width);
                    }
                    else
                    {
                        photo.SizeCM = string.Format("{0:0.##}", photo.Width * 2.54) + "X" +
                                       string.Format("{0:0.##}", photo.Height * 2.54);
                        photo.SizeInch = string.Format("{0:0.##}", photo.Width) + "X" +
                                         string.Format("{0:0.##}", photo.Height);
                    }
                }
            }
            gcPhoto.RefreshDataSource();

        }

        private void btnAplayDPIToAllPhoto_Click(object sender, EventArgs e)
        {
            PhotoDCT photo = gvPhoto.GetFocusedRow() as PhotoDCT;
            List<PhotoDCT> photoList = gvPhoto.DataSource as List<PhotoDCT>;
            foreach (var item in photoList)
            {
                item.DPI = photo.DPI;
                item.Width = item.PixWidth / item.DPI;
                item.Height = item.PixHeight / item.DPI;
                if (item.Height > item.Width)
                {
                    item.SizeCM = string.Format("{0:0.##}", item.Height * 2.54) + "X" +
                                       string.Format("{0:0.##}", item.Width * 2.54);
                    item.SizeInch = string.Format("{0:0.##}", item.Height) + "X" +
                                         string.Format("{0:0.##}", item.Width);
                }
                else
                {
                    item.SizeCM = string.Format("{0:0.##}", item.Width * 2.54) + "X" +
                                   string.Format("{0:0.##}", item.Height * 2.54);
                    item.SizeInch = string.Format("{0:0.##}", item.Width) + "X" +
                                     string.Format("{0:0.##}", item.Height);
                }
            }
            gcPhoto.RefreshDataSource();
            MessageBox.Show("应用成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);

        }

    }
}
